<?php 
	
?>
<?php
	include("include1.php");
	//ini_set('default_charset','utf-8');
	//header("Content-type: application/json; charset=utf-8");	
	$verb = $_SERVER["REQUEST_METHOD"];
	//echo $verb;
	if($verb == "GET")
	{
		
			header("Content-type: application/json; charset=utf-8");
			$arr = array();
			$sql = "";
			//$rs = mysql_query("select `id` as `HMid`, `TenHM`, `MaCT`, `Khac` from `danhsachhangmuc` where MaCT='" . $CTid . "'");
			if(isset($_REQUEST["congtrinh"]))
			{
				$sql = sprintf("select `id` as `HMid`, `TenHM`, `MaCT`, `Khac` from `danhsachhangmuc` where `MaCT` = '%s'", $_REQUEST["congtrinh"]);	
			}
			else
			{
				$sql = 	sprintf("select `id` as `HMid`, `TenHM`, `MaCT`, `Khac` from `danhsachhangmuc`");
			}
			$rs = mysql_query($sql);
			$temp="";
			while($obj = mysql_fetch_object($rs))
			{
					$arr[] = $obj;
					//$arr[]->TENCV = $obj->TENCV;
					//$temp=$obj->TENCV;
			}
			//echo $temp. "\n";
			//echo $arr[2]->TENCV;
			// add the header line to specify that the content type is JSON
			//echo "Dồ ngu";
			//echo json_encode($arr[2]->TENCV);
			echo "{\"data\":" . json_encode($arr , JSON_UNESCAPED_UNICODE) . "}";
		
	}
	
	if($verb == "POST")
	{
		$HMid = mysql_real_escape_string($_POST["HMid"]);
		$TenHM = mysql_real_escape_string($_POST["TenHM"]);
		$Khac = mysql_real_escape_string($_POST["Khac"]);
		
		$sql = sprintf("update danhsachhangmuc set TenHM = '%s', Khac = '%s' where id = '%s'", $TenHM, $Khac, $HMid);
		$rs = mysql_query($sql);
		
		if ($rs) {
        echo json_encode($rs);
		}
		else {
			header("HTTP/1.1 500 Internal Server Error");
			echo "Không thể cập nhật công trình: " .$TenCT;
		}
	}
	
	
	
?>